import Utils from "../Utils";
import SdkCode from "./SdkCode";
import SdkUtils from "./SdkUtils";

export default class Sdk_wx{
    private mBannerAd=null;
    private mVideoAd=null;
    private mInterstitialAd=null;
    private mGridAd=null;
    private mCustomAd=null;
    private mSdkIns=null;

    private mInterstitialAdId="";
    private mRewardAdIds=[];
    private mBannerAdIds=[];

    private mVideoAdCall:Function=null;
    private mInterstitialAdCall:Function=null;
    private mShareClickTime:number=0;
    private mShareCall:Function=null;
     private mOpenDataKey:string="suanzhu"
    constructor(){
        this.mSdkIns=window["wx"];
        if(this.mSdkIns){
            let tself = this;
            this.mSdkIns.showShareMenu({
                withShareTicket: true,
                 menus: ['shareAppMessage', 'shareTimeline']
            });
            this.mSdkIns.onHide((res)=>{

            });
            this.mSdkIns.onShow((res)=>{
                if(this.mShareCall){
                    let time = new Date().getTime()-tself.mShareClickTime;
                    tself.mShareCall(time>3000?SdkCode.DONE:SdkCode.SDK_SHOW_FAIL);
                    tself.mShareCall=null;
                }
            });
            if(SdkUtils.compareVersion(this.getSdkVersion(),"2.0.4")>=0){
                let tid = this.getReawardAdId(0);
                if(tid){
                    this.mVideoAd = this.mSdkIns.createRewardedVideoAd({adUnitId:tid});
                    this.mVideoAd.onError(res=>{
                        console.log("createRewardedVideoAd err",res.errMsg);
                        if(this.mVideoAdCall)this.mVideoAdCall(SdkCode.SDK_SHOW_FAIL)
                    });
                    this.mVideoAd.onLoad(()=>{this.mVideoAd.load()});
                    this.mVideoAd.onClose(()=>{if(this.mVideoAdCall)this.mVideoAdCall(SdkCode.CLOSE)});
                }
            }

            if(SdkUtils.compareVersion(this.getSdkVersion(),"2.6.0")>=0){
                let tid = this.mInterstitialAdId;
                if(tid){
                    this.mInterstitialAd = this.mSdkIns.createInterstitialAd({adUnitId:tid});
                    this.mInterstitialAd.onError(res=>{
                        console.log("createInterstitialAd err",res.errMsg);
                        if(this.mInterstitialAdCall)this.mInterstitialAdCall(SdkCode.SDK_SHOW_FAIL)
                    });
                    this.mInterstitialAd.onLoad(()=>{this.mInterstitialAd.load();});
                    this.mInterstitialAd.onClose(()=>{if(this.mInterstitialAdCall)this.mInterstitialAdCall(SdkCode.CLOSE)});
                }
            }
        }
    }

    public getShowSplashAdStatus(func:Function=null){
        if(!this.mSdkIns){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return ;
        }
        if(SdkUtils.compareVersion(this.getSdkVersion(),"3.7.8")<0){
            if(func)func(SdkCode.SDK_VERSION_TO_LOW);
            return ;
        }
        this.mSdkIns.getShowSplashAdStatus({
            success:res=>{if(func)(res.code==1?func(SdkCode.DONE):func(SdkCode.SDK_SHOW_FAIL))},
            fail:res=>{if(func)func(SdkCode.SDK_SHOW_FAIL)}
        });
    }

    public  showVideoAd(func:Function=null){
        if(!this.mVideoAd){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return;
        }
        this.mVideoAdCall=func;
        this.mVideoAd.load();
    }

    public showInterstitialAd(func:Function=null){
        if(!this.mInterstitialAd){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return;
        }
        this.mInterstitialAdCall=func;
        this.mInterstitialAd.load();
    }

    public showBannerAd(param,func:Function=null){
        if(!this.mSdkIns){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return ;
        }
        if(SdkUtils.compareVersion(this.getSdkVersion(),"2.0.4")<0){
            if(func)func(SdkCode.SDK_VERSION_TO_LOW);
            return 0;
        }
        if(!this.mBannerAd){
            let tadid = this.getBannerAdId(0);
            if(!tadid){
                if(func)func(SdkCode.NOT_FIND_SDK_INS);
                return;
            }
            this.mBannerAd=this.mSdkIns.createBannerAd({adUnitId:tadid,adIntervals:30,style:param});
            this.mBannerAd.onResize(res=>{
                let tw=res.width;
                let th=res.height;
            });
        }
        this.mBannerAd.show();
    }

    public getSdkVersion(){
        let tver = "1.0.0";
        if(this.mSdkIns)tver=this.mSdkIns.getSystemInfoSync().SDKVersion;
        return tver;
    }

    private getReawardAdId(index:number=0){
        if(this.mRewardAdIds.length){
            if(index>=0)return this.mRewardAdIds[index];
            else return this.mRewardAdIds[Math.floor(Math.random()*this.mRewardAdIds.length)];
        }
        return null;
    }

    private getBannerAdId(index:number=0){
        if(this.mBannerAdIds.length){
            if(index>0)return this.mBannerAdIds[index];
            else return this.mBannerAdIds[Math.floor(Math.random()*this.mBannerAdIds.length)];
        }
        return null;
    }


    public share(param,func:Function=null){
        if(!this.mSdkIns){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return;
        }
        this.mSdkIns.shareAppMessage(param);
        this.mShareClickTime=new Date().getTime();
        if(!this.mShareCall)this.mShareCall=func;
    }

    public showShareMenu(func:Function=null){
        if(this.mSdkIns){
            this.mSdkIns.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            })
            setTimeout(() => {
                if(func)func(SdkCode.DONE)
            }, 2000);
        }else{
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
        }
    }

    public getShareInfo(func:Function=null){
        if(!this.mSdkIns){
            if(func)func(SdkCode.NOT_FIND_SDK_INS);
            return;
        }
        if(Utils.compareVersion(this.getSdkVersion(),"2.10.4")<0) if(func)func(SdkCode.SDK_VERSION_TO_LOW);
        else this.mSdkIns.getGroupEnterInfo({allowSingleChat:false,needGroupOpenID:false,success:res=>{if(func)func(SdkCode.DONE)},fail:()=>{if(func)func(SdkCode.SDK_SHOW_FAIL)}});
    }

    public getUserInfo(func:Function){
        if(!this.mSdkIns){
            if(func)func(null);
            return;
        }
        let tself = this;
        this.mSdkIns.getSetting({
            success (res){
              if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                tself.mSdkIns.getUserInfo({
                  success: function(res) {
                    func(res.userInfo)
                  },
                  fail:function(){func(null)}
                })
              } else {
                // 否则，先通过 wx.createUserInfoButton 接口发起授权
                let button = tself.mSdkIns.createUserInfoButton({
                  type: 'text',
                  text: '获取用户信息',
                  style: {
                    left: 10,
                    top: 76,
                    width: 200,
                    height: 40,
                    lineHeight: 40,
                    backgroundColor: '#ff0000',
                    color: '#ffffff',
                    textAlign: 'center',
                    fontSize: 16,
                    borderRadius: 4
                  }
                })
                button.onTap((res) => {
                  // 用户同意授权后回调，通过回调可获取用户头像昵称信息
                  func(res.userInfo)
                })
              }
            }
          })
    }

    public login(func:Function){
        if(!this.mSdkIns){
            if(func)func(null);
            return;
        }
        this.mSdkIns.login({
            success (res) {
              if (res.code) {
                func(res);
              } else {
                console.log('登录失败！' + res.errMsg)
                func(null);
              }
            }
          })
    }

    public postOpenMessage(data){
        if(this.mSdkIns){
            let openDataContext = this.mSdkIns.getOpenDataContext()
            openDataContext.postMessage(data);
        }
    }

    public setUserCloudStorage(type:string,event:string,data){
        let tdata = {type:type,event:event,KVDataList:[{key:this.mOpenDataKey,value:JSON.stringify(data)}]};
        this.postOpenMessage(tdata);
    }
}